/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hr.cmi.android.aanwezigheidsregistratie;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import nl.hr.cmi.android.aanwezigheidsregistratie.helper.WebserviceWrapper;

/**
 *
 * @author HP-laptop
 */
public class Handmatigeinvoer extends Activity {

    Button button_selectall;
    Button button_selectnone;

    @Override
    public void onCreate(Bundle icycle) {
        super.onCreate(icycle);

        this.setContentView(R.layout.handmatigeinvoer);

        final TableLayout tabel = (TableLayout) findViewById(R.id.studentenlijst);
        TableRow rij = new TableRow(this);
        //rij.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        button_selectall = new Button(this);
        button_selectall.setText("Selecteer allen");
        rij.addView(button_selectall);
        button_selectnone = new Button(this);
        button_selectnone.setText("Deselecteer alles");
        rij.addView(button_selectnone);
        tabel.addView(rij, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


        TableLayout tl = (TableLayout) findViewById(R.id.studentenlijst);
        HashMap<String, String> map = WebserviceWrapper.getStudenten("INF1V"); // TODO Alleen INF1V geimplementeerd op het moment.
        for (String studentnummer : map.keySet()) {
            TableRow tr = new TableRow(this);
            tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            TextView text = new TextView(this);
            text.setText(studentnummer + " - " + map.get(studentnummer));
            //Onderstaande regel laat iets crashen
            //text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            tr.addView(text);

            CheckBox check = new CheckBox(this);
            check.setChecked(true);
            tr.addView(check);
            tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }

        TableRow rijonderaan = new TableRow(this);
        //rij.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        Button button_opslaan = new Button(this);
        button_opslaan.setText("Opslaan");
        rijonderaan.addView(button_opslaan);
        tabel.addView(rijonderaan, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


        button_selectall.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                TableLayout tl = (TableLayout) findViewById(R.id.studentenlijst);
                for (int i = 0; i < tl.getChildCount(); i++) {
                    if (tl.getChildAt(i) instanceof TableRow) {
                        TableRow tr = (TableRow) tl.getChildAt(i);
                        if (tr.getChildAt(1) instanceof CheckBox) {
                            CheckBox check = (CheckBox) tr.getChildAt(1);
                            check.setChecked(true);
                        }
                    }
                }
            }
        });
        button_selectnone.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                TableLayout tl = (TableLayout) findViewById(R.id.studentenlijst);
                for (int i = 0; i < tl.getChildCount(); i++) {
                    if (tl.getChildAt(i) instanceof TableRow) {
                        TableRow tr = (TableRow) tl.getChildAt(i);
                        if (tr.getChildAt(1) instanceof CheckBox) {
                            CheckBox check = (CheckBox) tr.getChildAt(1);
                            check.setChecked(false);
                        }
                    }
                }
            }
        });

        final List<String> namen = new ArrayList<String>();
        button_opslaan.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View view) {
                TableLayout tl = (TableLayout) findViewById(R.id.studentenlijst);
                for (int i = 0; i < tl.getChildCount(); i++) {
                    if (tl.getChildAt(i) instanceof TableRow) {
                        TableRow tr = (TableRow) tl.getChildAt(i);
                        if (tr.getChildAt(1) instanceof CheckBox) {
                            CheckBox check = (CheckBox) tr.getChildAt(1);
                            if (check.isChecked()) {
                                TextView naam = (TextView) tr.getChildAt(0);
                                namen.add(naam.getText() + "");
                            }
                        }
                    }
                }
                Log.e("NAMEN:", namen.toString());
            }
        });

    }
}
